import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_cwhisky/common/viewmodel/global_controller.dart';
import 'package:flutter_cwhisky/net/http_service.dart';
import 'package:flutter_cwhisky/pages/splash/splash_logic.dart';
import 'package:flutter_cwhisky/routes/app_pages.dart';
import 'package:flutter_cwhisky/services/auth_service.dart';
import 'package:flutter_cwhisky/utils/sp_utils.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:overlay_support/overlay_support.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'common/config/http_config_impl.dart';
import 'common/style/strings.dart';
import 'common/theme_controller.dart';
import 'common/utils/status_util.dart';
import 'pages/splash/splash_view.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top]);
  await SpUtil.getInstance();
  HttpService.init(HttpConfigImpl());
  if (kReleaseMode) {
    await dotenv.load(fileName: 'assets/config/.env');
  } else {
    await dotenv.load(fileName: 'assets/config/.env.debug');
  }
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final themeVm = Get.put(ThemeController());
  final globalVm = Get.put(GlobalController());

  MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    StatusBarUtils.setTransparentBar();
    return GetBuilder<ThemeController>(builder: (ThemeController themeController) {
      return ScreenUtilInit(
        designSize: const Size(375, 778),
        builder: (context, child) => RefreshConfiguration(
          hideFooterWhenNotFull: true,
          child: OverlaySupport(
            child: GetMaterialApp(
              title: AppStrings.appName,
              theme: themeController.getTheme(),
              getPages: AppPages.routes,
              initialBinding: BindingsBuilder(() => [
                    Get.lazyPut(() => AuthService()),
                    Get.lazyPut(() => SplashLogic()),
                  ]),
              builder: FlutterSmartDialog.init(),
              home: SplashPage(),

              // here
            ),
          ),
        ),
      );
    });
  }
}
